Threading
The ODBC specification requires that all ODBC drivers must be thread-safe; that is, they must not fail when database requests are made on separate threads.
Threading Architecture
An ODBC driver can be based on one of the following architectures:
- Not thread-safe. The ODBC driver should not be used in a multi-threaded environment.
- Thread-impaired. The ODBC driver serializes all ODBC calls. All requests are handled one by one, without concurrent processing.
- Thread per connection. The ODBC driver processes requests concurrently with statement handles that do not share the same connection handle; however requests on the same connection are serialized.
- Fully threaded. All requests fully use the threaded model. The ODBC driver processes all requests on multiple statements concurrently.
The ODBC driver supports multithreaded applications on all platforms.
Cancelling Functions in Multithreaded Applications
In a multithreaded application, the application can cancel a function that is running synchronously on a statement. To cancel the function, the application calls SQLCancel with the same statement handle as that used by the target function, but on a different thread. Whether SQLCancel actually cancels the running function depends on the data store being accessed as shown in Table 2-7.
In both cases, SQLCancel returns
SQL_SUCCESS. If SQLCancel has been called from a different thread while there is a pending request, the original statement will returnSQL_ERRORwith the error messageOperation cancelled.